package com.yjt.javatest.controller;



import com.yjt.javatest.entity.Category;
import com.yjt.javatest.service.CategoryService;
import com.yjt.javatest.utils.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("category")
@Tag(name = "题目分类")
@PreAuthorize("@ex.isTeacher()")
public class CategoryController {
    @Resource
    private CategoryService categoryService;


    @Operation(summary = "创建新分类", description = "给分类名和父分类id")
    @PostMapping("createCategory")
    public APIResponse createCategory(@RequestBody Category category) {
        return categoryService.createCategory(category);
    }

    @Operation(summary = "根据父分类id列出当前用户的所有分类",description = "fatherId为0则为根分类")
    @GetMapping("listByFatherId")
    public APIResponse listCategoriesByFatherId(@RequestParam Integer fatherId) throws Exception {
        return categoryService.listCategoriesByFatherId(fatherId);
    }

    @Operation(summary = "修改分类", description = "用于修改分类名字，修改父id")
    @PostMapping("updateCategory")
    public APIResponse updateCategory(@RequestBody Category category) {
        if(categoryService.updateById(category)) {
            return APIResponse.success("修改成功");
        } else {
            return APIResponse.error("修改失败");
        }
    }

    @PostMapping("{id}")
    @Operation(summary = "根据id删除分类", description = "删除分类将级联删除其子分类,并将所属题目设置为未分类")
    public APIResponse deleteCategory(@PathVariable Integer id) {
        return categoryService.deleteCategoryById(id);

    }

    @GetMapping("/listAll")
    @Operation(summary = "获取用户所有分类及其题目",description = "")
    public APIResponse listAll() {
        return categoryService.listAll();
    }


}

